﻿/*----------------------------------------------------------------
//   Copyright (C) 2012 YMSOFT All rights reserved 
//
//   CLR版本:   2.0.50727.3634
//   Namespace: BP.BLL
//   FileName : B_Model
//   Description: 
// 
//   Created by YAOMING at 2012-10-25 11:02:33
//   http://www.loverer.com QQ:78867011
//
//   Modified: 
//   Modify description: 
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ART.Data;
using System.Data;
using System.Data.SqlClient;

namespace ART.BLL
{
    public class B_Model
    {
        /// <summary>
        /// 获取记录条数
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="strWheres"></param>
        /// <returns></returns>
        public static int GetRecordCount(string TableName,string strWheres)
        {
            return SqlHelper.GetRecordCount(TableName, strWheres);
        }
        /// <summary>
        /// 返回DataTable
        /// </summary>
        /// <param name="strCmd"></param>
        /// <returns></returns>
        public static DataTable GetDataTable(string strCmd)
        {
            return SqlHelper.ExecDataTable(strCmd,new SqlParameter[0]);
        }
        /// <summary>
        /// 获取单个值
        /// </summary>
        /// <param name="strCmd"></param>
        /// <returns></returns>
        public static object GetSingle(string strCmd)
        {
            return SqlHelper.ExecScalar(strCmd, new SqlParameter[0]);
        }
        /// <summary>
        /// 获取ID集合（以 , 分隔）
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="OrderBy"></param>
        /// <param name="TableName"></param>
        /// <param name="strWheres"></param>
        /// <returns></returns>
        public static string GetIdList(string FileName, string OrderBy, string TableName, string strWheres)
        {
            return GetIdList(0, FileName, OrderBy, TableName, strWheres);
        }
        /// <summary>
        /// 获取前N条 ID集合（以 , 分隔）
        /// </summary>
        /// <param name="Top"></param>
        /// <param name="FileName"></param>
        /// <param name="OrderBy"></param>
        /// <param name="TableName"></param>
        /// <param name="strWheres"></param>
        /// <returns></returns>
        public static string GetIdList(int Top, string FileName, string OrderBy, string TableName, string strWheres)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strRe = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" " + FileName + " FROM " + TableName);
            if (strWheres.Trim() != "")
            {
                strSql.Append(" where " + strWheres);
            }
            if (OrderBy.Trim() != "")
            {
                strSql.Append(" order by " + OrderBy);
            }
            DataSet ds = SqlHelper.ExecDataSet(strSql.ToString());
            if (ds!=null&&ds.Tables[0]!=null&&ds.Tables[0].Rows.Count>0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    strRe.Append(dr[0]).Append(",");
                }
                ds = null;
            }
            return strRe.ToString().Trim(',');
        }
    }
}
